package com.stu.mapstruct.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.Test;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * @Author: MI
 * @Date: 2021/9/9/23:25
 * @Description: https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-lombok
 */
public class Demo {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Source {
        private String testing;
        private String a;
        private String id;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Target {
        private String test;
        private String a;
        private int id;

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
        }
    }

    @Mapper
    public static interface SourceTargetMapper {
        SourceTargetMapper MAPPER = Mappers.getMapper(SourceTargetMapper.class);

        @Mapping(target = "test", source = "testing")
        Target toTarget(Source s);
    }

    @Test
    public void test() {
        Source source = new Source();
        source.setTesting("test");
        source.setA("a");
        source.setId("1");

        Target target = SourceTargetMapper.MAPPER.toTarget(source);
        System.out.println(target);
    }
}
